package edu.washington.tcss360.burningpenguins.BPGUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerListModel;
import javax.swing.SpinnerModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import edu.washington.tcss360.burningpenguins.data.Role;

// methods in here to change the functionality of the main panel
/**
 * The primary panel of the GUI is constructed within this class.
 * 
 * @author Sean
 * @version 1
 * @since 11/19/2013
 */
public class BPContentPage extends JPanel {

	/**
	 * Enables serializing of this object.
	 */
	private static final long serialVersionUID = 6671070420735940394L;

	/**
	 * Burning penguins icon.
	 */
	private BufferedImage image;
	
	private final BPFrame my_frame;
	
	/**
	 * Constructor for the main content page.
	 */
	protected BPContentPage(final BPFrame frame) {
		super();
		my_frame = frame;
		BP_panel();
		//login_panel();
		this.setBackground(Color.LIGHT_GRAY);
		this.setPreferredSize(new Dimension(650, 600));
		this.setVisible(true);
	}

	
	public void display(String type) {
		switch (type) {
			case "login":
				System.err.println("Inside display login command");
				login_panel();
				break;
			case "submit":
				submission_panel();
				break;
			default:
				BP_panel();
		}
		this.repaint();
		
	}	
	
	private void submission_panel() {
		this.removeAll();
		this.setBackground(Color.GRAY);
		
		this.setVisible(true);
	}
	
	private void login_panel() {
		this.removeAll();
		this.setLayout(new BorderLayout());
		String[] roleList = {"Program Chair", "SubProgram Chair", "Reviewer", "Author", "User"};
		SpinnerListModel loginModel = new SpinnerListModel(roleList);
		final JSpinner login_roles = new JSpinner(loginModel);
		login_roles.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent arg0) {
				SpinnerModel temp = login_roles.getModel();
				int target_role = 0;
				switch((String)temp.getValue()) {
				case "Program Chair":
					target_role = Role.PROGRAM_CHAIR; break;
				case "SubProgram Chair":
					target_role = Role.SUBPROGRAM_CHAIR; break;
				case "Reviewer":
					target_role = Role.REVIEWER; break;
				case "Author":
					target_role = Role.AUTHOR; break;
				default:
					target_role = 0;
				}
				my_frame.switchRole(target_role);				
			}
			
		});
		
		JFormattedTextField ftf = getTextField(login_roles);
		if(ftf != null) {
			ftf.setColumns(30);
			ftf.setHorizontalAlignment(JTextField.LEFT);
		}
		
		this.add(new JLabel("Please select a role to log in as:"), BorderLayout.CENTER);
		this.add(login_roles, BorderLayout.SOUTH);
		//this.setVisible(true);
		//repaint();
	}
	
	private void BP_panel() {
		this.removeAll();
		this.setBackground(Color.GRAY);
		try {
			// this is mainly for comic relief.  If we do get an actual icon for this, replace reference here.
			image = ImageIO.read(new File("setFire.jpg"));
			JLabel bpIcon = new JLabel(new ImageIcon(image));
			this.setLayout(new BorderLayout());
			this.add(bpIcon);
		} catch (IOException e) {
			
		}
		this.setVisible(true);
	}
	
	/**
	 * Pulled from spinner tutorial.
	 * 
	 * @param spinner
	 * @return
	 */
	public JFormattedTextField getTextField(JSpinner spinner) {
	    JComponent editor = spinner.getEditor();
	    if (editor instanceof JSpinner.DefaultEditor) {
	        return ((JSpinner.DefaultEditor)editor).getTextField();
	    } else {
	        System.err.println("Unexpected editor type: "
	                           + spinner.getEditor().getClass()
	                           + " isn't a descendant of DefaultEditor");
	        return null;
	    }
	}
}
